package cn.edu.ctbu.myfirstweb.controller;

import cn.edu.ctbu.myfirstweb.domain.Student;
import cn.edu.ctbu.myfirstweb.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

@Slf4j
@Controller
public class mycontroller {

    @Autowired
    StudentService studentService;

    @RequestMapping("/Html/login")
    public String login() {
        return "/Html/login";
    }

    /**
     * 学生总表显示页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/Html/administrator/list")
    public String list(Model model) {
        List<Student> students = studentService.findAll();
        model.addAttribute("data", students);
        return "/Html/administrator/list";
    }

    /**
     * 查询结果显示若查找成功则跳转到结果显示页面，若失败则跳转到error页面报错
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/student/query")
    public String query(HttpServletRequest request, Model model) {
        String search_input = request.getParameter("NameQuery");
        Student student = studentService.findByName(search_input);
        if (student != null) {
            model.addAttribute("search_result", student);
            return "/student/stu";
        } else {
            return "/student/error";
        }
    }


}
